package vno.fux.res.manager
{
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.IOErrorEvent;
	import flash.events.ProgressEvent;
	
	import vno.fux.BaseDisplayObject;
	import vno.fux.GlobalVar;
	import vno.fux.res.pool.MapResource;
	import vno.fux.res.pool.ResourcePool;
	import vno.fux.utils.IMissionDispatcher;
	import vno.fux.utils.Mission;
	import vno.fux.utils.MissionManager;
	
	public class ResourceManager extends EventDispatcher
	{
		internal static var loader:LoaderManager;
		
		private static var instance:ResourceManager;
		private static var _missionId:uint = 0;
		
		private var _settings:XML;
		private var _loaderNum:uint = 4;

		private var _resourcePool:ResourcePool;
		private var _mapManager:MapResourceManager;
		private var _loaderManager:LoaderManager;
		private var _characterManager:CharacterResourceManager;
		private var _uiManager:UiResourceManager;
		private var _soundManager:SoundResourceManager;

		
		public function ResourceManager(pvt:PrivateClass, settings_:XML)
		{
			_resourcePool = ResourcePool.getInstance();
			_settings = settings_;
			
			_loaderManager = new LoaderManager(8);
			ResourceManager.loader = _loaderManager;
			
			MapResourceManager.init(createMapSetting(settings_));
			_mapManager = MapResourceManager.getInstance();
			
			CharacterResourceManager.init(createCharacterSetting(settings_));
			_characterManager= CharacterResourceManager.getInstance();
			
			UiResourceManager.init(createUiRes(settings_));
			_uiManager = UiResourceManager.getInstance();
			
			SoundResourceManager.init(createSndRes(settings_));
			_soundManager = SoundResourceManager.getInstance();
		}
		
		public function get resourcePool():ResourcePool
		{
			return _resourcePool;
		}
		
		public function set resourcePool(value:ResourcePool):void
		{
			_resourcePool = value;
		}

		
		public function get settings():XML
		{
			return _settings;
		}
		
		public function set settings(value:XML):void
		{
			_settings = value;
		}
		
		public function get map():MapResourceManager
		{
			return _mapManager;
		}

		public function get cha():CharacterResourceManager
		{
			return _characterManager;
		}
		public function get ui():UiResourceManager
		{
			return _uiManager;
		}
		public function get snd():SoundResourceManager
		{
			return _soundManager;
		}
		public function get loader():LoaderManager
		{
			return _loaderManager;
		}

		public static function init(settings_:XML):void
		{
			if(ResourceManager.instance)
			{
				throw new Error("You can't init the ResourceManager twice!")
			}
			ResourceManager.instance = new ResourceManager(new PrivateClass, settings_);
		}

		public static function getInstance():ResourceManager
		{
			return ResourceManager.instance;
		}
		
		private function createCharacterSetting(resourse:XML):XML//code by ky
		{
			var result:XML=<characterRes/>;
			result.appendChild(<assets/>);
			var temp:XML=<temp/>;
			temp.tempassets.assets=resourse.characterRes..assets;
			for(var i:int =0;temp.tempassets.assets[i]!=null;i++)
			{
				result.assets[i]=temp.tempassets.assets[i];
			}
			return result;
		}

		private function createMapSetting(resourse:XML):XML//code by ky
		{
			var mapRes:XML=<mapRes/>;
			for(var i:int =0;resourse.mapRes.map[i]!=null;i++)
			{
				mapRes.appendChild(<map/>);
				mapRes.map[i]=resourse.mapRes.map[i];
			}
			return mapRes;
		}
		private function createUiRes(resourse:XML):XML//code by ky
		{
			var result:XML=<soundRes/>;
			result.appendChild(<assets/>);
			var temp:XML=<temp/>;
			temp.tempassets.assets=resourse.uiRes..assets;
			for(var i:int =0;temp.tempassets.assets[i]!=null;i++)
			{
				result.assets[i]=temp.tempassets.assets[i];
			}
			return result;
		}
		private function createSndRes(resourse:XML):XML
		{
			var result:XML=<sndRes/>;
			result.appendChild(<assets/>);
			var temp:XML=<temp/>;
			temp.tempassets.assets=resourse.sndRes..assets;
			for(var i:int =0;temp.tempassets.assets[i]!=null;i++)
			{
				result.assets[i]=temp.tempassets.assets[i];
			}
			return result;
		}


	}
}

class PrivateClass
{
	public function PrivateClass()
	{
	}
}
